package com.ytktt.study.week9.task3.rpcfx.server.netty.handler;

import com.ytktt.study.week9.task3.rpcfx.api.RpcfxRequest;
import com.ytktt.study.week9.task3.rpcfx.api.RpcfxResponse;
import com.ytktt.study.week9.task3.rpcfx.server.RpcfxInvoker;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Netty server 消息读取处理
 *
 * @author ytk
 */
@Slf4j
@RequiredArgsConstructor
public class NettyServerHandler extends SimpleChannelInboundHandler<RpcfxRequest> {

    private final RpcfxInvoker rpcfxInvoker;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcfxRequest msg) throws Exception {
        log.info("server receive msg: {}", msg);
        RpcfxResponse response = this.rpcfxInvoker.invoke(msg);
        ctx.writeAndFlush(response);
    }

}
